package com.sl.spring.intergration.rabbitmq.header;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;

@Component
public class HeaderReciver {

	@Autowired
	private ConnectionFactory connectionFactory;

	private final static String exchange = "header-exchange";
	private final static String QUEUE_NAME = "header-queue";

	public void recive() {

		Connection connection = null;
		Channel channel = null;
		try {
			connection = connectionFactory.createConnection();
			channel = connection.createChannel(false);
			channel.exchangeDeclare(exchange, ExchangeTypes.HEADERS);
			channel.queueDeclare(QUEUE_NAME,false, false, true,null); 

			Map<String, Object> header = new HashMap<String, Object>();
			header.put("x-match", "any");
			header.put("testss", "testss");
			Builder properties = new BasicProperties.Builder();
			properties.headers(header);

			channel.queueBind(QUEUE_NAME, exchange, "", header);
			QueueingConsumer consumer = new QueueingConsumer(channel);
			channel.basicConsume(QUEUE_NAME, true, consumer);
			while (true) {
				QueueingConsumer.Delivery delivery = consumer.nextDelivery();
				String message = new String(delivery.getBody());
				System.out.println(message);
			}
		} catch (Exception e) {

		}
	}
}
